//
//  TestDownloadCell.m
//  ProjectDemo
//
//  Created by IOS3 on 2019/1/9.
//  Copyright © 2019 IOS3. All rights reserved.
//

#import "TestDownloadCell.h"
#import "MCDownloader.h"



@interface TestDownloadCell ()

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UIButton *startButton;


@end

@implementation TestDownloadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setUrl:(NSString *)url {
    _url = url;
    
    [self donloadWithURL:url];
}
- (IBAction)startButtonClick:(id)sender {
    
    MCDownloadReceipt *receipt = [[MCDownloader sharedDownloader] downloadReceiptForURLString:self.url];
    
    // 开始或者等待中
    __weak typeof(self)weakSelf = self;
    if (receipt.state == MCDownloadStateDownloading || receipt.state == MCDownloadStateWillResume) {
        [[MCDownloader sharedDownloader] cancel:receipt completed:^{
            [weakSelf.startButton setTitle:@"开始下载" forState:UIControlStateNormal];
        }];
    }
    else {
        [self donloadWithURL:self.url];
    }
}


- (void)donloadWithURL:(NSString *)url {
    
    __weak typeof(self)weakSelf = self;
    MCDownloadReceipt *receipt = [[MCDownloader sharedDownloader] downloadDataWithURL:[NSURL URLWithString:url] progress:^(NSInteger receivedSize, NSInteger expectedSize, NSInteger speed, NSURL * _Nullable targetURL) {
        CGFloat recived = receivedSize / 1024.f / 1024;
        CGFloat expected = expectedSize / 1024.f / 1024;
        CGFloat sp = speed / 1024.f;
        weakSelf.detailLabel.text = [NSString stringWithFormat:@"总文件: %.2fM   已下载: %.2fM,   速度: %.0fkb", expected, recived, sp];
        
    } completed:^(MCDownloadReceipt * _Nullable receipt, NSError * _Nullable error, BOOL finished) {
        NSLog(@"receipt.path: %@", receipt.filePath);
    }];
    
    receipt.customFilePathBlock = ^NSString * _Nullable(MCDownloadReceipt * _Nullable receipt) {
        NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        NSString *path = [[docPath stringByAppendingPathComponent:@"book"] stringByAppendingPathComponent:receipt.truename];
        return path;
    };
    
    
    if (receipt.state == MCDownloadStateDownloading) {
        [self.startButton setTitle:@"暂停" forState:UIControlStateNormal];
        
    }
    else if (receipt.state == MCDownloadStateWillResume) {
        [self.startButton setTitle:@"等待下载" forState:UIControlStateNormal];
    }
    else if (receipt.state == MCDownloadStateCompleted) {
        [self.startButton setTitle:@"完成" forState:UIControlStateNormal];
        
    }
    else {
        [self.startButton setTitle:@"开始" forState:UIControlStateNormal];
    }
    
    self.titleLabel.text = [NSString stringWithFormat:@"trueName: %@, fileName: %@ \n filePath: %@ \n url: %@", receipt.truename, receipt.filename, receipt.filePath, receipt.url];
}


@end
